#!/bin/bash
# source ./branch_info_table.sh
#:<<!
#获取一个罗列分支信息的表格
#
#Develop_Branchs_FILE_PATH="../example_packing_info/app_branch_info.json"
#buildContainBranchMaps=$(cat ${Develop_Branchs_FILE_PATH} | jq -r '.package_merger_branchs') # -r 去除字符串引号
#
#History_Versions_FILE_PATH="../example_packing_info/app_history_version.json"
#branchMapArray=$(cat ${History_Versions_FILE_PATH} | jq -r '.history_versions[0].online_brances')
#sh ./brances_info/brances_info_log/branch_info_table.sh "${branchMapArray}"
#!

JQ_EXEC=`which jq`

echo "\n进入 $0 脚本，执行获取一个罗列分支信息的表格"

CurrentDirName=${PWD##*/}


sep="#"
function append_cell(){
    #对表格追加单元格
    #append_cell col0 "col 1" ""
    #append_cell col3
    local i
    for i in "$@"
    do
        line+="$i${sep}"
    done
}
function check_line(){
if [ -n "$line" ]
then
    c_c=$(echo $line tr -cd "${sep}"|wc -c)
    difference=$((${column_count}-${c_c}))
    if [ $difference -gt 0 ]
    then
        line+=$(seq -s " " $difference|sed -r s/[0-9]\+/\|${sep}/g|sed -r  s/${sep}\ /${sep}/g)
    fi
    content+="${line}\n"
fi

}
function append_line(){
    check_line
    line=""
    local i
    for i in "$@"
    do
        line+="$i${sep}"
    done
    check_line
    line=""
}
function segmentation(){
    local seg=""
    local i
    for i in $(seq $column_count)
    do
        seg+="${sep}"
    done
    if [ ! -n "${seg}" ]
    then
    echo $seg
    fi
}
function set_title(){
    #表格标头，以空格分割，包含空格的字符串用引号，如
    #set_title Column_0 "Column 1" "" Column3
    [ -n "$title" ] && echo "Warring:表头已经定义过,重写表头和内容"
    column_count=0
    title=""
    local i
    for i in "$@"
    do
        title+="${i}${sep}"
        let column_count++
    done
    title+="\n"
    seg=`segmentation`
    title="${seg}${title}${seg}"
    content=""
}
function output_table(){
    if [ ! -n "${title}" ]
    then
        echo "未设置表头，退出" && return 1
    fi
    append_line
    table="${title}${content}$(segmentation)"
    echo $table|column -s "${sep}" -t|awk '{if($0 ~ /^+/){gsub(" "," ",$0);print $0}else{gsub("\\(\\*\\)","\033[31m(*)\033[0m",$0);print $0}}'

}
    
function ou_table() {
    branchMapArray=$1
    
    #echo "branchMapArray=${branchMapArray}"
    if [ -z "${branchMapArray}" ] || [ "${branchMapArray}" == "null" ]; then
        return 0
    fi
    
    set_title "序号" "分支名" "提测日期" "测试通过日期" "合入预生产日期"
        
    ##branchMapArray=(`echo ${branchMapArray}`)
    #echo "branchMapArray=${branchMapArray[*]}"
    #
    branchMapNameArray=$(echo "${branchMapArray}" | ${JQ_EXEC} -r '.[].name') # -r 去除字符串引号
    branchMapNameArray=($branchMapNameArray)    # 此时才是数组
    #echo "branchMapNameArray=${branchMapNameArray[*]}"
    branchMapCount=${#branchMapNameArray[*]}
    #echo "branchMapCount=${branchMapCount}"
    #echo "\n"

    #echo "branchMapArray=${branchMapArray[*]}"
    #echo "\n"
    branchMapArray="${branchMapArray}"
    for ((i=0;i<branchMapCount;i++))
    do
        
        branchName=$(echo ${branchMapArray} | ${JQ_EXEC} -r ".[$((i))].name") # -r 去除字符串引号
        
        testState='unknow'
                    #提交测试的时间
        branchSubmitTestTime=$(echo ${branchMapArray} | ${JQ_EXEC} -r ".[$((i))].submit_test_time") # -r 去除字符串引号
        if [ "${branchSubmitTestTime}" != "null" ] && [ -n "${branchSubmitTestTime}" ]; then
            testState='test_submit'
        fi
        
        #通过测试的时间
        branchPassTestTime=$(echo ${branchMapArray} | ${JQ_EXEC} -r ".[$((i))].pass_test_time") # -r 去除字符串引号
        if [ "${branchPassTestTime}" != "null" ] && [ -n "${branchPassTestTime}" ]; then
            testState='test_pass'
        fi
        
        #合入预生产的时间
        branchMergerPreproductTime=$(echo ${branchMapArray} | ${JQ_EXEC} -r ".[$((i))].merger_pre_time") # -r 去除字符串引号
        if [ "${branchMergerPreproductTime}" != "null" ] && [ -n "${branchMergerPreproductTime}" ]; then
            testState='test_prefect'
        fi
        
        if [ "${testState}" == "test_submit" ]; then
            branchPassTestTime='还在测试中'
            branchMergerPreproductTime='还在测试中'
        elif [ "${testState}" == "test_pass" ]; then
            branchMergerPreproductTime='待合入分支'
        elif [ "${testState}" == "test_prefect" ]; then
            abcdefg='123'
        else
            branchSubmitTestTime='待提测'
            branchPassTestTime='待提测'
            branchMergerPreproductTime='待提测'
        fi
        
        
        append_line $((i+1)) ${branchName} ${branchSubmitTestTime} ${branchPassTestTime} ${branchMergerPreproductTime}
    done

    output_table
}

    




:<<!
if [ "$SHLVL" -eq "2" ]
then
    set_title "序号" "分支名" "创建日期" "提测日期" "测试通过日期"
    append_line 1 "dev_in_pgyer" "11.07" "11.07" ""
    append_line 2 "dev_script_pack" "11.07" "11.07" "11.08"
    append_line 3 "dev_invoice_1107_he" "11.07" "11.07" "12.2"
    append_line 4 "dev_lzh_fix" "11.07" "11.07" ""
    append_line 5 "dev_update_dialog" "11.07" "11.07" ""
    # append_cell 2 "" "2017-01-02(*)"
    # append_line
    # append_cell 3 "SF"
    # append_line
    # append_cell 3 "SF" "(*)"
    # append_line 4 "TS"
    # append_cell 5
    output_table
fi
!

buildContainBranchMaps=$1
BranchTableInfoResult=`ou_table "${buildContainBranchMaps}"`
echo "BranchTableInfoResult=${BranchTableInfoResult}"
